﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XmlServer
{
    public class Server        
    {

        // we have 2 listeners:
        // 1. Processing Listener: this is the default listener used to process incoming client requests
        //                     all "regular" client requests will be processed through this listener
        // 2. Priority Listener: When you need a request to be processed fast (higher priority), the priority listener will be used.
        //      This is usefull for getting the server status, which will be used by our administration tools
        //      Priority Listener could also be used for load balancing....getting the status of multiple servers and relaying a request to the server that has the least work.
        private Listeners.Listener processingListener;
        private Listeners.Listener priorityListener;

        public  Server()
        {
            this.processingListener = new Listeners.Listener(Listeners.ListenerType.ProcessingListener);
            this.priorityListener = new Listeners.Listener(Listeners.ListenerType.PriorityListener);
            
            // allow the listeners to "see" each other
            this.processingListener.OtherListener = priorityListener;
            this.priorityListener.OtherListener = processingListener;
        }

      
      
        /// <summary>
        /// Gets the status of the client processing listener
        /// </summary>
        public Listeners.ListenerStatus ProcessorStatus
        {
            get { return this.processingListener.Status; }
        }

        /// <summary>
        /// Gets the status of the priority processing listener
        /// </summary>
        public Listeners.ListenerStatus PriorityStatus
        {
            get { return this.priorityListener.Status; }
        }



        public Listeners.Listener ProcessingListener
        {
            get { return processingListener; }
        }

        public Listeners.Listener PriorityListener
        {
            get { return priorityListener; }
        }


        /// <summary>
        /// Start the Processing Listener and the Priorty Listener
        /// </summary>
        public bool StartListening()
        {
            this.ProcessingListener.StartListener();
            this.PriorityListener.StartListener();

            return this.ProcessingListener.Status.Listening && this.PriorityListener.Status.Listening;
        }

        /// <summary>
        /// Stop the process and priority listeners
        /// </summary>
        public void StopListening()
        {
            this.ProcessingListener.StopListener();
            this.PriorityListener.StopListener();           
        }


        /// <summary>
        /// Start the Processing Listener and the Priorty Listener
        /// </summary>
        public bool PauseListening()
        {
            this.ProcessingListener.PauseListener();
            this.PriorityListener.PauseListener();

            return this.ProcessingListener.Status.Paused && this.PriorityListener.Status.Paused;
        }


        /// <summary>
        /// Resume paused listeners
        /// </summary>
        public void ResumeListening()
        {
            this.ProcessingListener.ResumeListener();
            this.PriorityListener.ResumeListener();
        }

        /// <summary>
        /// Shutdown the process and priority listeners and abort any queued client requests
        /// </summary>
        public void Shutdown()
        {            
            this.ProcessingListener.ShutDown();
            this.PriorityListener.ShutDown();
        }

        public bool Listening
        {
            get
            {
                return this.PriorityListener.Status.Listening && this.ProcessingListener.Status.Listening;
            }
        }

        public bool Paused
        {
            get
            {
                return this.PriorityListener.Status.Paused && this.ProcessingListener.Status.Paused;
            }
        }

    }
}
